import Vue from 'vue'
import $g from './index.js'

/* 价格格式化 */
function formatPrice(price, integral) {
    let result = ''
    if (price && price > 0) {
        result += '￥' + (price * 1).toFixed(2)
        if (integral && integral > 0) {
            result += '+'
        }
    }
    if (integral && integral > 0) {
        result += integral + $g.integralUnit
    }

    return result
}

/* 格式化数字 */
function formatNumber(num, money = 0) {
    if (typeof num !== 'number' && typeof num !== 'string') {
        num = 0
    }
    num = Number(num)
    switch (true) {
        case num > 9999 && num < 100000:
            return (num / 1000).toFixed(1) + 'k'
            break
        case num > 99999 && num < 10000000:
            return (num / 10000).toFixed(1) + 'w'
            break
        case num > 9999999:
            return (num / 10000000).toFixed(1) + 'kw'
            break
        case money === 1:
            return num.toFixed(2)
            break
        default:
            return num
            break;
    }
}

/* 格式化积分 */
function formatIntegral(value, num=2) {
    let result=' '
    if(Number(value)>0){
        result=Number(value).toFixed(num)
    }
    
    return result
}

function initTime(value, type = 1) {
    if (value && type == 1) {
        let str = value.split(' ')
        return str[0]
    } else if (value && type == 2) {
        let str = value.substr(5, 11)
        return str 
    } else {
        return value
    }

}
function formatGoodsTag(value) {
    if(value.length>4){
        let str=value.slice(0,3)+'...'
        return str
    }else{
        return value
    }
    

}
Vue.filter('formatPrice', formatPrice)
Vue.filter('formatNumber', formatNumber)
Vue.filter('initTime', initTime)
Vue.filter('formatIntegral', formatIntegral)
Vue.filter('formatGoodsTag', formatGoodsTag)